/* interfaces.c -- Contains funcs to construct the gui interface
 *
 * Time-stamp: <2007-06-28 06:35:54 BRT Diogo F. S. Ramos>
 *
 *
 */

#include <gtk/gtk.h>
#include <stdlib.h>
#include <glade/glade.h>

#include "../includes/engcur.h"
#include "../includes/callbacks.h"
#include "../includes/interfaces.h"

GtkWidget*
initial_setup( GladeXML *xmlTree, semester *sem )
{
    GtkWidget *main_window = NULL;
    GtkWidget *widget = NULL;
    GtkWidget *widgetaux = NULL;
    GtkWidget *dialog = NULL;
    GtkWidget *treeview_disciplines = NULL;
    GtkWidget *treeview_schedule = NULL;
    GtkTreeViewColumn *column = NULL;
    GtkCellRenderer *renderer = NULL;
    GtkTreeStore *store_disciplines = NULL;
    GtkTreeStore *store_schedule = NULL;
    GtkTreeModel *sortmodel_schedule = NULL;
    FILE **file = (FILE**) malloc( sizeof(FILE*) );
    *file = NULL;
    char **filename = (char**) malloc( sizeof(char*) );
    *filename = NULL;
    
    /* widget:main_window */
    main_window = glade_xml_get_widget( xmlTree, "main_window" );
    g_signal_connect( G_OBJECT( main_window ), "delete-event",
		      G_CALLBACK( quit_program ), NULL );
    g_object_set_data( G_OBJECT( main_window ), "filename",
		       filename );


    /* widget:treeview_disciplines */
    treeview_disciplines = glade_xml_get_widget( xmlTree, "treeview_disciplines" );
    store_disciplines = gtk_tree_store_new( DISCIPLINE_N_COLUMN,
					    G_TYPE_STRING,
					    G_TYPE_STRING,
					    G_TYPE_STRING,
					    G_TYPE_STRING,
					    G_TYPE_STRING,
					    G_TYPE_STRING,
					    G_TYPE_STRING);
    gtk_tree_view_set_model( GTK_TREE_VIEW( treeview_disciplines ),
			     GTK_TREE_MODEL( store_disciplines ) );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Código",
						       renderer,
						       "text",
						       DISCIPLINE_COD,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Turma",
						       renderer,
						       "text",
						       DISCIPLINE_CLASS,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Nome",
						       renderer,
						       "text",
						       DISCIPLINE_NAME,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Créditos",
						       renderer,
						       "text",
						       DISCIPLINE_CREDITS,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
			 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Dia",
						       renderer,
						       "text",
						       DISCIPLINE_DAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
			 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Início",
						       renderer,
						       "text",
						       DISCIPLINE_BEGIN,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
			 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Fim",
						       renderer,
						       "text",
						       DISCIPLINE_END,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_disciplines ),
			 column );

    /* widget:treeview_schedule */
    treeview_schedule = glade_xml_get_widget( xmlTree, "treeview_schedule" );
    store_schedule = gtk_tree_store_new( SCHEDULE_N_COLUMN,
					 G_TYPE_INT,
					 G_TYPE_INT,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING,
					 G_TYPE_STRING);
    sortmodel_schedule = gtk_tree_model_sort_new_with_model(
	GTK_TREE_MODEL(store_schedule) );
    gtk_tree_sortable_set_sort_column_id( GTK_TREE_SORTABLE( sortmodel_schedule ),
					  SCHEDULE_BEGIN_INT, GTK_SORT_ASCENDING );
    gtk_tree_view_set_model( GTK_TREE_VIEW( treeview_schedule ),
			     GTK_TREE_MODEL( sortmodel_schedule ) );

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Início",
						       renderer,
						       "text",
						       SCHEDULE_BEGIN,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Fim",
						       renderer,
						       "text",
						       SCHEDULE_END,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Domingo",
						       renderer,
						       "text",
						       SCHEDULE_SUNDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Segunda",
						       renderer,
						       "text",
						       SCHEDULE_MONDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Terça",
						       renderer,
						       "text",
						       SCHEDULE_TUESDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Quarta",
						       renderer,
						       "text",
						       SCHEDULE_WEDNESDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Quinta",
						       renderer,
						       "text",
						       SCHEDULE_THURSDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Sexta",
						       renderer,
						       "text",
						       SCHEDULE_FRIDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes( "Sábado",
						       renderer,
						       "text",
						       SCHEDULE_SATURDAY,
						       NULL);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview_schedule ),
				 column );

    /* widget:dialog_about */
    dialog = glade_xml_get_widget( xmlTree, "dialog_about" );
    g_signal_connect( G_OBJECT( dialog ), "delete-event",
		      G_CALLBACK( std_event_handler ), NULL);
    g_signal_connect( GTK_WIDGET( dialog ), "response",
		      G_CALLBACK( std_dialog_handler ), NULL );
    g_object_set_data( G_OBJECT( dialog ), "handler",
		       GINT_TO_POINTER( DIALOG_ABOUT ) );

    /* widget:menuitem_about */
    widget = glade_xml_get_widget( xmlTree, "menuitem_about" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_ABOUT ) );
    g_object_set_data( G_OBJECT( widget ), "dialog_about",
		       dialog );

    /* widget:dialog_new */
    dialog = glade_xml_get_widget( xmlTree, "dialog_new" );
    g_signal_connect( G_OBJECT( dialog ), "delete-event",
		      G_CALLBACK( std_event_handler ), NULL);
    g_signal_connect( GTK_WIDGET( dialog ), "response",
		      G_CALLBACK( std_dialog_handler ), NULL );
    g_object_set_data( G_OBJECT( dialog ), "handler",
		       GINT_TO_POINTER( DIALOG_NEW ) );
    g_object_set_data( G_OBJECT( dialog ), "store_disciplines",
		       store_disciplines );
    g_object_set_data( G_OBJECT( dialog ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( dialog ), "filename",
		       filename );
    g_object_set_data( G_OBJECT( dialog ), "semester",
		       sem );

    /* widget:menuitem_new */
    widget = glade_xml_get_widget( xmlTree, "menuitem_new" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_NEW ) );
    g_object_set_data( G_OBJECT( widget ), "dialog_new",
		       dialog );

    /* widget:dialog_filechooser_save */
    dialog = glade_xml_get_widget( xmlTree, "dialog_filechooser_save" );
    g_signal_connect( G_OBJECT( dialog ), "delete-event",
		      G_CALLBACK( std_event_handler ), NULL);
    g_signal_connect( GTK_WIDGET( dialog ), "response",
		      G_CALLBACK( std_dialog_handler ), NULL );
    g_object_set_data( G_OBJECT( dialog ), "handler",
		       GINT_TO_POINTER( DIALOG_FILECHOOSER_SAVE ) );
    g_object_set_data( G_OBJECT( dialog ), "filename",
		       filename );
    g_object_set_data( G_OBJECT( dialog ), "semester",
		       sem );

    /* widget:menuitem_save_as */
    widget = glade_xml_get_widget( xmlTree, "menuitem_save_as" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_SAVE_AS ) );
    g_object_set_data( G_OBJECT( widget ), "dialog_filechooser_save",
		       dialog );
   
    /* widget:menuitem_save */
    widget = glade_xml_get_widget( xmlTree, "menuitem_save" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_SAVE ) );
    g_object_set_data( G_OBJECT( widget ), "filename",
		       filename );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );

    /* widget:dialog_filechooser_load */
    dialog = glade_xml_get_widget( xmlTree, "dialog_filechooser_load" );
    g_signal_connect( G_OBJECT( dialog ), "delete-event",
		      G_CALLBACK( std_event_handler ), NULL);
    g_signal_connect( GTK_WIDGET( dialog ), "response",
		      G_CALLBACK( std_dialog_handler ), NULL );
    g_object_set_data( G_OBJECT( dialog ), "handler",
		       GINT_TO_POINTER( DIALOG_FILECHOOSER_LOAD ) );
    g_object_set_data( G_OBJECT( dialog ), "store_disciplines",
		       store_disciplines );
    g_object_set_data( G_OBJECT( dialog ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( dialog ), "filename",
		       filename );
    g_object_set_data( G_OBJECT( dialog ), "semester",
		       sem );

    /* widget:menuitem_load */
    widget = glade_xml_get_widget( xmlTree, "menuitem_load" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_LOAD ) );
    g_object_set_data( G_OBJECT( widget ), "dialog_filechooser_load",
		       dialog );

    /* widget:menuitem_quit */
    widget = glade_xml_get_widget( xmlTree, "menuitem_quit" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_QUIT ) );
    g_object_set_data( G_OBJECT( widget ), "filename",
		       filename );

    /* widget:menuitem_print_table */
    widget = glade_xml_get_widget( xmlTree, "menuitem_print_table" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_PRINT_TABLE ) );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );		       

    /* widget:button_disc_add */
    widget = glade_xml_get_widget( xmlTree, "button_disc_add" );
    g_signal_connect( G_OBJECT( widget ), "clicked",
		      G_CALLBACK( std_button_handler ), NULL );
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( BUTTON_DISC_ADD ) );
    widgetaux = glade_xml_get_widget( xmlTree, "entry_disc_cod" );
    g_object_set_data( G_OBJECT( widget ), "entry_disc_cod",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "entry_disc_name" );
    g_object_set_data( G_OBJECT( widget ), "entry_disc_name",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "spinbutton_disc_credits" );
    g_object_set_data( G_OBJECT( widget ), "spinbutton_disc_credits",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "spinbutton_disc_class" );
    g_object_set_data( G_OBJECT( widget ), "spinbutton_disc_class",
		       widgetaux );
    g_object_set_data( G_OBJECT( widget ), "store_disciplines",
		       store_disciplines );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );

    /* widget:button_disc_rem */
    widget = glade_xml_get_widget( xmlTree, "button_disc_rem" );
    g_signal_connect( G_OBJECT( widget ), "clicked",
		      G_CALLBACK( std_button_handler ), NULL );
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( BUTTON_DISC_REM ) );
    g_object_set_data( G_OBJECT( widget ), "treeview_disciplines",
		       treeview_disciplines );
    g_object_set_data( G_OBJECT( widget ), "store_disciplines",
		       store_disciplines );
    g_object_set_data( G_OBJECT( widget ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );
    
    /* widget:button_disc_add_seed */
    widget = glade_xml_get_widget( xmlTree, "button_disc_add_seed" );
    g_signal_connect( G_OBJECT( widget ), "clicked",
		      G_CALLBACK( std_button_handler ), NULL );
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( BUTTON_DISC_ADD_SEED ) );
    widgetaux = glade_xml_get_widget( xmlTree, "combobox_disc_day" );
    g_object_set_data( G_OBJECT( widget ), "combobox_disc_day",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "spinbutton_disc_begin_hour" );
    g_object_set_data( G_OBJECT( widget ), "spinbutton_disc_begin_hour",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "spinbutton_disc_begin_minute" );
    g_object_set_data( G_OBJECT( widget ), "spinbutton_disc_begin_minute",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "spinbutton_disc_end_hour" );
    g_object_set_data( G_OBJECT( widget ), "spinbutton_disc_end_hour",
		       widgetaux );
    widgetaux = glade_xml_get_widget( xmlTree, "spinbutton_disc_end_minute" );
    g_object_set_data( G_OBJECT( widget ), "spinbutton_disc_end_minute",
		       widgetaux );
    g_object_set_data( G_OBJECT( widget ), "store_disciplines",
		       store_disciplines );
    treeview_disciplines = glade_xml_get_widget( xmlTree, "treeview_disciplines" );
    g_object_set_data( G_OBJECT( widget ), "treeview_disciplines",
		       treeview_disciplines );
    g_object_set_data( G_OBJECT( widget ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );

    /* widget:button_disc_rem_seed */
    widget = glade_xml_get_widget( xmlTree, "button_disc_rem_seed" );
    g_signal_connect( G_OBJECT( widget ), "clicked",
		      G_CALLBACK( std_button_handler ), NULL );
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( BUTTON_DISC_REM_SEED ) );
    g_object_set_data( G_OBJECT( widget ), "store_disciplines",
		       store_disciplines );
    treeview_disciplines = glade_xml_get_widget( xmlTree, "treeview_disciplines" );
    g_object_set_data( G_OBJECT( widget ), "treeview_disciplines",
		       treeview_disciplines );
    g_object_set_data( G_OBJECT( widget ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );

    /* widget:combobox_disc_day */
    widget = glade_xml_get_widget( xmlTree, "combobox_disc_day" );
    gtk_combo_box_set_active( GTK_COMBO_BOX( widget ), 1 );

    /* widget:menuitem_schedule_disc_add */
    widget = glade_xml_get_widget( xmlTree, "menuitem_schedule_disc_add" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_SCHEDULE_DISC_ADD ) );
    g_object_set_data( G_OBJECT( widget ), "treeview_disciplines",
		       treeview_disciplines );
    g_object_set_data( G_OBJECT( widget ), "store_disciplines",
		       store_disciplines );
    g_object_set_data( G_OBJECT( widget ), "treeview_schedule",
		       treeview_schedule );
    g_object_set_data( G_OBJECT( widget ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( widget ), "sortmodel_schedule",
		       sortmodel_schedule );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );

    /* widget:menuitem_schedule_disc_add */
    widget = glade_xml_get_widget( xmlTree, "menuitem_schedule_disc_rem" );
    g_signal_connect( G_OBJECT( widget ), "activate",
        	      G_CALLBACK( std_button_handler), NULL);
    g_object_set_data( G_OBJECT( widget ), "handler",
		       GINT_TO_POINTER( MENUITEM_SCHEDULE_DISC_REM ) );
    g_object_set_data( G_OBJECT( widget ), "treeview_disciplines",
		       treeview_disciplines );
    g_object_set_data( G_OBJECT( widget ), "store_disciplines",
		       store_disciplines );
    g_object_set_data( G_OBJECT( widget ), "treeview_schedule",
		       treeview_schedule );
    g_object_set_data( G_OBJECT( widget ), "store_schedule",
		       store_schedule );
    g_object_set_data( G_OBJECT( widget ), "sortmodel_schedule",
		       sortmodel_schedule );
    g_object_set_data( G_OBJECT( widget ), "semester",
		       sem );

    return main_window;
}
